package com.jinzhi.jzweb.controller;


import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.CertificateNewsDO;
import com.jinzhi.jzweb.domain.KnowledgePointsDO;
import com.jinzhi.jzweb.domain.TemplateDetailsDO;
import com.jinzhi.jzweb.domain.TestPaperTemplateDO;
import com.jinzhi.jzweb.service.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * <pre>
 * 模板详情
 * </pre>
 * <small> 2020-02-24 21:00:12 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/templateDetails")
public class TemplateDetailsController extends BaseController {
    @Autowired
    private TemplateDetailsService templateDetailsService;
    @Autowired
    private KnowledgePointsService knowledgePointsService;
    @Autowired
    private TestPaperTemplateService testPaperTemplateService;
    @Autowired
    private CertificateNewsService certificateNewsService;
    @Autowired
    private ItemBankService itemBankService;

    @GetMapping("/{id}")
    @RequiresPermissions("jzweb:templateDetails:templateDetails")
    String TemplateDetails(@PathVariable("id") Long id, Model model) {
        model.addAttribute("tptemplateId", id);
        TestPaperTemplateDO testPaperTemplateDO = testPaperTemplateService.selectById(id);
        CertificateNewsDO certificateNewsDO = certificateNewsService.selectById(testPaperTemplateDO.getNewsId());
        if(certificateNewsDO != null){
            Map map1 = new HashMap();

            map1.put("profileId",certificateNewsDO.getProfileId());
            if(certificateNewsDO.getGrade() == 0){
                map1.put("grade","高级");
            }else if(certificateNewsDO.getGrade() == 1){
                map1.put("grade","中级");
            }else if(certificateNewsDO.getGrade() == 2){
                map1.put("grade","初级");
            }
            List<KnowledgePointsDO> knowledgePointsDO = knowledgePointsService.selByProidGrade(map1);
            model.addAttribute("totalScore",testPaperTemplateDO.getTotalScore());

            if (knowledgePointsDO != null && knowledgePointsDO.size() > 0) {
                List<Integer> type = new ArrayList<>();
                List<Integer> titleNumber = new ArrayList<>();
                List<Integer> score = new ArrayList<>();
                for (int i = 0; i < knowledgePointsDO.size(); i++) {
                    TemplateDetailsDO templateDetailsDO = templateDetailsService.findOneByKv("tptemplate_id", id, "kpoints_id", knowledgePointsDO.get(i).getId(), "type", 0);
                    type.add(0);
                    if (templateDetailsDO != null) {
                        titleNumber.add(templateDetailsDO.getTitleNumber());
                        score.add(templateDetailsDO.getScore());
                    } else {
                        titleNumber.add(null);
                        score.add(null);
                    }
                    TemplateDetailsDO templateDetailsDO1 = templateDetailsService.findOneByKv("tptemplate_id", id, "kpoints_id", knowledgePointsDO.get(i).getId(), "type", 1);
                    type.add(1);
                    if (templateDetailsDO1 != null) {
                        titleNumber.add(templateDetailsDO1.getTitleNumber());
                        score.add(templateDetailsDO1.getScore());
                    } else {
                        titleNumber.add(null);
                        score.add(null);
                    }
                    TemplateDetailsDO templateDetailsDO2 = templateDetailsService.findOneByKv("tptemplate_id", id, "kpoints_id", knowledgePointsDO.get(i).getId(), "type", 2);
                    type.add(2);
                    if (templateDetailsDO2 != null) {
                        titleNumber.add(templateDetailsDO2.getTitleNumber());
                        score.add(templateDetailsDO2.getScore());
                    } else {
                        titleNumber.add(null);
                        score.add(null);
                    }
                    TemplateDetailsDO templateDetailsDO3 = templateDetailsService.findOneByKv("tptemplate_id", id, "kpoints_id", knowledgePointsDO.get(i).getId(), "type", 3);
                    type.add(3);
                    if (templateDetailsDO3 != null) {
                        titleNumber.add(templateDetailsDO3.getTitleNumber());
                        score.add(templateDetailsDO3.getScore());
                    } else {
                        titleNumber.add(null);
                        score.add(null);
                    }
                    TemplateDetailsDO templateDetailsDO4 = templateDetailsService.findOneByKv("tptemplate_id", id, "kpoints_id", knowledgePointsDO.get(i).getId(), "type", 4);
                    type.add(4);
                    if (templateDetailsDO4 != null) {
                        titleNumber.add(templateDetailsDO4.getTitleNumber());
                        score.add(templateDetailsDO4.getScore());
                    } else {
                        titleNumber.add(null);
                        score.add(null);
                    }
                    TemplateDetailsDO templateDetailsDO5 = templateDetailsService.findOneByKv("tptemplate_id", id, "kpoints_id", knowledgePointsDO.get(i).getId(), "type", 5);
                    type.add(5);
                    if (templateDetailsDO5 != null) {
                        titleNumber.add(templateDetailsDO5.getTitleNumber());
                        score.add(templateDetailsDO5.getScore());
                    } else {
                        titleNumber.add(null);
                        score.add(null);
                    }
                }

            for(int i=0;i<type.size();i++){
                System.out.print(type.get(i)+"  ");
            }
                System.out.println();
            for(int i=0;i<titleNumber.size();i++){
                System.out.print(titleNumber.get(i)+"  ");
            }
                System.out.println();
            for(int i=0;i<score.size();i++){
                System.out.print(score.get(i)+"  ");
            }
                model.addAttribute("type", type);
                model.addAttribute("titleNumber", titleNumber);
                model.addAttribute("score", score);
            } else {
                model.addAttribute("type", "");
                model.addAttribute("titleNumber", "");
                model.addAttribute("score", "");
            }
        } else {
            model.addAttribute("type", "");
            model.addAttribute("titleNumber", "");
            model.addAttribute("score", "");
        }


        return "jzweb/templateDetails/templateDetails";
    }

    @Log("修改模板详情")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("jzweb:templateDetails:edit")
    public Result<String> update(String tptemplateId, String[] kpointsId, String[] type, String[] titleNumber, String[] score) {
        if (StringUtils.isBlank(tptemplateId)) {
            return Result.fail("缺少参数试卷模板id");
        }

        if (kpointsId != null && kpointsId.length > 0) {
//            for(int i=0;i<kpointsId.length;i++){
//                System.out.print(kpointsId[i]+"  ");
//            }
//            for(int i=0;i<type.length;i++){
//                System.out.print(type[i]+"  ");
//            }
//            for(int i=0;i<titleNumber.length;i++){
//                System.out.print(titleNumber[i]+"  ");
//            }
//            for(int i=0;i<score.length;i++){
//                System.out.print(score[i]+"  ");
//            }
            int nn = 0;
            List<TemplateDetailsDO> templateDetailsDOS = new ArrayList<>();
            List<Long> temIds = new ArrayList<>();
            for (int i = 0; i < type.length; i++) {
                TemplateDetailsDO templateDetailsDO = templateDetailsService.findOneByKv("tptemplate_id", Long.parseLong(tptemplateId), "kpoints_id", kpointsId[nn], "type", type[i]);
                if (StringUtils.isNotBlank(titleNumber[i]) && StringUtils.isNotBlank(score[i])) {
                    if (templateDetailsDO != null) {
                        templateDetailsDO.setTitleNumber(Integer.parseInt(titleNumber[i]));//题目数量
                        templateDetailsDO.setScore(Integer.parseInt(score[i]));//题目分值
                        templateDetailsDOS.add(templateDetailsDO);
                    } else {
                        TemplateDetailsDO templateDetailsDO1 = new TemplateDetailsDO();
                        templateDetailsDO1.setTptemplateId(Long.parseLong(tptemplateId));//试卷模板id
                        templateDetailsDO1.setKpointsId(Long.parseLong(kpointsId[nn]));//知识点id
                        templateDetailsDO1.setType(Integer.parseInt(type[i]));//题目类型
                        templateDetailsDO1.setTitleNumber(Integer.parseInt(titleNumber[i]));//题目数量

                        templateDetailsDO1.setScore(Integer.parseInt(score[i]));//题目分值
                        templateDetailsDO1.setState(0);//默认正常
                        templateDetailsDO1.setCreaterId(this.getUserId());//创建人id
                        templateDetailsDO1.setCreaterDate(new Date());//创建时间
                        templateDetailsDOS.add(templateDetailsDO1);
                    }

                } else if (StringUtils.isBlank(titleNumber[i]) && StringUtils.isBlank(score[i])) {
                    if (templateDetailsDO != null) {
                        temIds.add(templateDetailsDO.getId());
                    }
                }else{
                    KnowledgePointsDO knowledgePointsDO = knowledgePointsService.selectById(kpointsId[nn]);
                    String typess;
                    if(type[i].equals("0")){
                        typess = "单选";
                    }else if(type[i].equals("1")){
                        typess = "多选";
                    }else{
                        typess = "判断";
                    }
                    return Result.fail("知识点(" + knowledgePointsDO.getName() + ")中题目类型("+ typess + ")的题目数量或分值为空，请确保某一题目类型中两个值都填或都不填！");
                }

                if ((i + 1) % 3 == 0) {
                    nn = nn + 1;
                }
            }


            if (templateDetailsDOS.size() > 0) {
                boolean cc = templateDetailsService.insertOrUpdateBatch(templateDetailsDOS, templateDetailsDOS.size());
                if (!cc) {
                    return Result.fail("添加模板失败");
                }
            }

            if (temIds.size() > 0) {
                boolean ee = templateDetailsService.deleteBatchIds(temIds);
                if (!ee) {
                    return Result.fail("删除模板失败");
                }
            }

            return Result.ok();


        } else {
            return Result.fail("暂无知识点");
        }

    }



    @ResponseBody
    @RequestMapping("/knowss")
    public Result<?> knowss(String tptemplateId) {
        if (StringUtils.isBlank(tptemplateId)) {
            return Result.fail("缺少参数试卷模板id");
        }
        List<KnowledgePointsDO> knowledgePointsDO = new ArrayList<>();
        TestPaperTemplateDO testPaperTemplateDO = testPaperTemplateService.selectById(tptemplateId);
        if(testPaperTemplateDO != null){
            CertificateNewsDO certificateNewsDO = certificateNewsService.selectById(testPaperTemplateDO.getNewsId());
            if(certificateNewsDO != null){
                Map map1 = new HashMap();
                map1.put("profileId",certificateNewsDO.getProfileId());
                if(certificateNewsDO.getGrade() == 0){
                    map1.put("grade","高级");
                }else if(certificateNewsDO.getGrade() == 1){
                    map1.put("grade","中级");
                }else if(certificateNewsDO.getGrade() == 2){
                    map1.put("grade","初级");
                }
                knowledgePointsDO = knowledgePointsService.selByProidGrade(map1);
            }
        }
        for(int i=0;i<knowledgePointsDO.size();i++){
            List<Map<Object,Object>> itemNum = itemBankService.selNumByKpoints(knowledgePointsDO.get(i).getId());
            for(Map map : itemNum){
                if(map.get("itemBankType").equals(0)){
                    if (map.get("topicTypes").equals(0)) {
                        knowledgePointsDO.get(i).setSingleNum0(Integer.parseInt(map.get("num").toString()));
                    }else if (map.get("topicTypes").equals(1)) {
                        knowledgePointsDO.get(i).setMultipleNum0(Integer.parseInt(map.get("num").toString()));
                    }else if (map.get("topicTypes").equals(4)) {
                        knowledgePointsDO.get(i).setMultipleNum4(Integer.parseInt(map.get("num").toString()));
                    }else{
                        knowledgePointsDO.get(i).setJudgeNum0(Integer.parseInt(map.get("num").toString()));
                    }
                }else{
                    if (map.get("topicTypes").equals(0)) {
                        knowledgePointsDO.get(i).setSingleNum1(Integer.parseInt(map.get("num").toString()));
                    }else if (map.get("topicTypes").equals(1)) {
                        knowledgePointsDO.get(i).setMultipleNum1(Integer.parseInt(map.get("num").toString()));
                    }else if (map.get("topicTypes").equals(4)) {
                        knowledgePointsDO.get(i).setMultipleNum4(Integer.parseInt(map.get("num").toString()));
                    }else{
                        knowledgePointsDO.get(i).setJudgeNum1(Integer.parseInt(map.get("num").toString()));
                    }
                }

            }
        }

        return Result.ok(knowledgePointsDO);

    }

}
